package com.sgb.kjwl.viewmodel.self.userInfo;

import android.util.Base64;

import com.im.immodel.IMUtils;
import com.sgb.kjwl.AppConstant;
import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.repository.SelfRepertory;
import com.sgb.kjwl.view.event.self.ChangePwdEvent;
import com.sgb.kjwl.view.event.self.MachinePublishEvent;
import com.sgb.kjwl.view.ui.activity.self.userInfo.ChangePwdActivity;
import com.sgb.kjwl.view.ui.activity.self.userInfo.VerificationCodeLoginActivity;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.event.EventConstant;
import com.swgk.core.util.MLog;
import com.swgk.core.util.MToast;
import com.swgk.core.util.SharedPreferenceUtil;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.functions.Consumer;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ChangePwdModel extends BaseViewModel {
    private ChangePwdActivity activity;
    private SelfRepertory repertory;

    public ChangePwdModel(ChangePwdActivity activity, SelfRepertory repertory) {
        this.activity = activity;
        this.repertory = repertory;
    }

    public void saveModel(String oldPwd, String newPwd) {
        Map<String, String> map = new HashMap<>();
        map.put("oldpass", oldPwd);
        map.put("newpass", newPwd);

        repertory.changePwd(map)
                .subscribe(new Consumer<BaseEntity>() {
                    @Override
                    public void accept(BaseEntity baseEntity) throws Exception {

                        if ("ok".equals(baseEntity.getState())) {
                            MToast.showToast(activity, baseEntity.getData().toString());
                        } else {
                            MToast.showToast(activity, baseEntity.getData().toString());
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        MLog.d(AppConstant.API_LOG, throwable.getMessage());
                    }
                });
    }

    public void changePwd(String oldPwd, String newPwd) {
        APIManager.getInstance().getUserAPI().changePwd(Base64.encodeToString(oldPwd.getBytes(), Base64.DEFAULT), Base64.encodeToString(newPwd.getBytes(), Base64.DEFAULT)).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                if (response.body() == null) return;
                if ("ok".equals(response.body().getState())) {
                    MToast.showToast(activity, response.body().getData().toString());
                    SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.LOGIN_TOKEN, "");
                    SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.LOGIN_PHONE, "");
                    IMUtils.getInstance().clearLoginUserInfo();
                    EventBus.getDefault().postSticky(new ChangePwdEvent("OK"));
                    activity.finish();
                    VerificationCodeLoginActivity.start(activity);
                } else {
                    MToast.showToast(activity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {

            }
        });
    }
}
